//
//  CalenderView.m
//  Qihuo
//
//  Created by Vencoo on 13-9-27.
//  Copyright (c) 2013年 Vencoo. All rights reserved.
//
#define viewWidth 984
#define viewHeight 624
#define inter 10

#import "CalenderView.h"
#import "NSDate+Calendar.h"
#import "CaculateDate.h"
#import <QuartzCore/QuartzCore.h>

@implementation CalenderView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
       
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{  
    
    CGContextRef line = UIGraphicsGetCurrentContext();
   
    CGContextSetFillColorWithColor(line, [UIColor colorWithRed:39/255.0 green:104/255.0 blue:182/255.0 alpha:1].CGColor);
    CGContextAddRect(line, CGRectMake(inter*2, self.meett.yearLabel.frame.size.height + inter *2, viewWidth - inter *4 - 6, inter * 5));
    CGContextDrawPath(line, kCGPathFillStroke);
    
    CGContextSetLineWidth(line, 1);
    CGContextSetStrokeColorWithColor(line, [UIColor grayColor].CGColor);
    CGContextMoveToPoint(line,inter *2, self.meett.yearLabel.bounds.origin.y + self.meett.yearLabel.bounds.size.height + inter *2);
    CGContextAddLineToPoint(line,viewWidth - (inter * 2.5),self.meett.yearLabel.bounds.origin.y + self.meett.yearLabel.bounds.size.height + inter *2);
    CGContextDrawPath(line, kCGPathStroke);

    NSDate *date = [NSDate new];
    date = [NSDate getDateWithString:[NSString stringWithFormat:@"%04d-%02d-%02d",[_meett.yearLabel.text intValue] ,[_meett.monthLabel.text intValue],date.day] DateFormat:@"yyyy-MM-dd"];
    CaculateDate *cal = [CaculateDate new];
//    NSString *today = [NSString stringWithFormat:@"%u",date.day];
//    八条竖线
    for (int i = 0; i<8; i++) {
        CGContextSetLineWidth(line, 1);
        CGContextSetStrokeColorWithColor(line, [UIColor grayColor].CGColor);
        CGContextMoveToPoint(line,inter*2+(viewWidth - inter *4)/7*i, self.meett.yearLabel.bounds.origin.y + self.meett.yearLabel.bounds.size.height + inter *2);
        CGContextAddLineToPoint(line,inter*2+(viewWidth - inter *4)/7*i,viewHeight-inter*2);
        CGContextDrawPath(line, kCGPathStroke);
    }
    switch ([cal weekNumber:date]) {
        case 4:
            {
            for (int j=0; j<6; j++) {
                CGContextSetLineWidth(line, 1);
                CGContextSetStrokeColorWithColor(line, [UIColor grayColor].CGColor);
                CGContextMoveToPoint(line,inter * 2, (viewHeight - self.meett.monthLabel.bounds.size.height - inter * 9)/5*j+self.meett.monthLabel.bounds.size.height + inter *7);
                CGContextAddLineToPoint(line,viewWidth - inter *2.5,(viewHeight - self.meett.monthLabel.bounds.size.height - inter * 9)/5*j+self.meett.monthLabel.bounds.size.height + inter *7);
                CGContextDrawPath(line, kCGPathStroke);
                //单元格高104.8
            }
//                for (NSString *day in [cal dayInMonth:date])
//                {
//                    if ([day intValue] == date.day) {
//                        CGContextSetFillColorWithColor(line, [UIColor redColor].CGColor);
//                    }else{
//                        CGContextSetFillColorWithColor(line, [UIColor blackColor].CGColor);
//                    }
//                    date = [NSDate getDateWithString:[NSString stringWithFormat:@"%04d-%02d-%02d",[_meett.yearLabel.text intValue] ,[_meett.monthLabel.text intValue],[day intValue]] DateFormat:@"yyyy-MM-dd"];
//                    [day drawAtPoint:CGPointMake(((date.weekday-1)*138)+80,(int)((date.day+[cal weekdayOfFirstDay:date]-2)/7)*104.8+180) forWidth:100 withFont:[UIFont systemFontOfSize:35] lineBreakMode:NSLineBreakByWordWrapping];
//                }
            }
                           break;
         case 5:
         {
           for (int j=0; j<6; j++) {
               CGContextSetLineWidth(line, 1);
               CGContextSetStrokeColorWithColor(line, [UIColor grayColor].CGColor);
               CGContextMoveToPoint(line,inter * 2, (viewHeight - self.meett.monthLabel.bounds.size.height - inter * 9)/5*j+self.meett.monthLabel.bounds.size.height + inter *7);
               CGContextAddLineToPoint(line,viewWidth - inter *2.5,(viewHeight - self.meett.monthLabel.bounds.size.height - inter * 9)/5*j+self.meett.monthLabel.bounds.size.height + inter *7);
               CGContextDrawPath(line, kCGPathStroke);
         }
        
//        for (NSString *day in [cal dayInMonth:date]) {
//            if ([day intValue] == [today intValue]) {
//                CGContextSetFillColorWithColor(line, [UIColor redColor].CGColor);
//            }else{
//                CGContextSetFillColorWithColor(line, [UIColor blackColor].CGColor);
//            }
//            date = [NSDate getDateWithString:[NSString stringWithFormat:@"%04d-%02d-%02d",[_meett.yearLabel.text intValue],[_meett.monthLabel.text intValue],[day intValue]] DateFormat:@"yyyy-MM-dd"];
//            [day drawAtPoint:CGPointMake(((date.weekday-1)*138)+80,(int)((date.day+[cal weekdayOfFirstDay:date]-2)/7)*104.8+180) forWidth:100 withFont:[UIFont systemFontOfSize:35] lineBreakMode:NSLineBreakByWordWrapping];
//             CGContextDrawPath(line, kCGPathStroke);
//        }
    }
    break;
        case 6:
        {
            for (int j=0; j<7; j++) {
                CGContextSetLineWidth(line, 1);
                CGContextSetStrokeColorWithColor(line, [UIColor grayColor].CGColor);
                CGContextMoveToPoint(line,inter * 2, (viewHeight - self.meett.monthLabel.bounds.size.height - inter * 9)/6*j+self.meett.monthLabel.bounds.size.height + inter *7);
                CGContextAddLineToPoint(line,viewWidth - inter *2.5,(viewHeight - self.meett.monthLabel.bounds.size.height - inter * 9)/6*j+self.meett.monthLabel.bounds.size.height + inter *7);
                CGContextDrawPath(line, kCGPathStroke);
            }
//            for (NSString *day in [cal dayInMonth:date]) {
//                if ([day intValue] == [today intValue]) {
//                    CGContextSetFillColorWithColor(line, [UIColor redColor].CGColor);
//                }else{
//                    CGContextSetFillColorWithColor(line, [UIColor blackColor].CGColor);
//                }
//                date = [NSDate getDateWithString:[NSString stringWithFormat:@"%04d-%02d-%02d",[_meett.yearLabel.text intValue],[_meett.monthLabel.text intValue],[day intValue]] DateFormat:@"yyyy-MM-dd"];
//                [day drawAtPoint:CGPointMake(((date.weekday-1)*138)+80,(int)((date.day+[cal weekdayOfFirstDay:date]-2)/7)*90+180) forWidth:100 withFont:[UIFont systemFontOfSize:35] lineBreakMode:NSLineBreakByWordWrapping];
//            }
        }
            break;
}
//
//   CGContextSetFillColorWithColor(line, [UIColor whiteColor].CGColor);
//   NSString *sunday = @"星期日";
//   [sunday drawAtPoint:CGPointMake(20, 100) forWidth:138 withFont:[UIFont systemFontOfSize:44] lineBreakMode:NSLineBreakByWordWrapping];
//   NSString *monday = @"星期一";
//   [monday drawAtPoint:CGPointMake(168, 100) forWidth:138 withFont:[UIFont systemFontOfSize:44] lineBreakMode:NSLineBreakByWordWrapping];
//   NSString *tuesday = @"星期二";
//   [tuesday drawAtPoint:CGPointMake(306, 100) forWidth:138 withFont:[UIFont systemFontOfSize:44] lineBreakMode:NSLineBreakByWordWrapping];
//   NSString *wednsday = @"星期三";
//   [wednsday drawAtPoint:CGPointMake(444, 100) forWidth:138 withFont:[UIFont systemFontOfSize:44] lineBreakMode:NSLineBreakByWordWrapping];
//   NSString *thursday = @"星期四";
//   [thursday drawAtPoint:CGPointMake(582, 100) forWidth:138 withFont:[UIFont systemFontOfSize:44] lineBreakMode:NSLineBreakByWordWrapping];
//   NSString *friday = @"星期五";
//   [friday drawAtPoint:CGPointMake(720, 100) forWidth:138 withFont:[UIFont systemFontOfSize:44] lineBreakMode:NSLineBreakByWordWrapping];
//   NSString *satarday = @"星期六";
//   [satarday drawAtPoint:CGPointMake(858, 100) forWidth:138 withFont:[UIFont systemFontOfSize:44] lineBreakMode:NSLineBreakByWordWrapping];
}
@end
